//
//  GRContactCertificationViewCell.m
//  YC_GRice
//
//  Created by LiuFeng on 2018/7/24.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRContactCertificationViewCell.h"


@implementation GRContactCertificationViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _realNameTF.limitNum = 16; _realNameTF.delegate = self;
    _mobileTF.limitNum   = 19; _mobileTF.delegate   = self;
}

- (IBAction)chooseContact:(id)sender {
    if (@available(iOS 9.0, *)) {
        CNContactPickerViewController *contactPicker = [[CNContactPickerViewController alloc] init];
        contactPicker.delegate = self;
        contactPicker.displayedPropertyKeys = @[CNContactPhoneNumbersKey];
        [self.currentVc presentViewController:contactPicker animated:YES completion:nil];
    } else {
        // Fallback on earlier versions
        ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
        peoplePicker.peoplePickerDelegate = self;
        [self.currentVc presentViewController:peoplePicker animated:YES completion:nil];
    }
}

-(void)textField:(UITextField *)textField textDidChange:(NSString *)text {
    BOOL isFinish = NO;
    if (self.realNameTF.text.length > 0 && self.mobileTF.text.length >= 11) {
        isFinish = YES;
    }
    if ([self.delegate respondsToSelector:@selector(didselectContactPerson:AtCell:)]) {
        [self.delegate didselectContactPerson:isFinish AtCell:self];
    }
}

#pragma Mark  ============  <ABPeoplePickerNavigationControllerDelegate, CNContactPickerDelegate>   =============
-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(nonnull CNContactProperty *)contactProperty  API_AVAILABLE(ios(9.0)){
    [self.currentVc dismissViewControllerAnimated:YES completion:^{
        NSString *name = [NSString stringWithFormat:@"%@%@",contactProperty.contact.familyName,contactProperty.contact.givenName];
        if (!name || name.length == 0) {
            name = contactProperty.contact.organizationName;
        }
        NSString *mobile = @"";
        if ([contactProperty.value isKindOfClass:[CNPhoneNumber class]]) {
            CNPhoneNumber *phoneNumber = (CNPhoneNumber *)contactProperty.value;
            mobile = phoneNumber.stringValue;
        }else {
            [GRHUD showMessage:@"请选择手机号"];
        }

        [self fillINContact:name mobile:mobile];
    }];
}

-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0) {
    ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef,identifier);
    CFStringRef value = ABMultiValueCopyValueAtIndex(valuesRef,index);
    CFStringRef anFullName = ABRecordCopyCompositeName(person);
    [self.currentVc dismissViewControllerAnimated:YES completion:^{
        NSString *name = [NSString stringWithFormat:@"%@",anFullName];
        NSString *mobile = (__bridge NSString*)value;
        
        [self fillINContact:name mobile:mobile];
    }];
    CFRelease(valuesRef);
}

- (void)fillINContact:(NSString *)name mobile:(NSString *)mobile {
    if (name && mobile) {
        _realNameTF.text = name;
        _mobileTF.text   = mobile;
        if ([self.delegate respondsToSelector:@selector(didselectContactPerson:AtCell:)]) {
            [self.delegate didselectContactPerson:YES AtCell:self];
        }
    }else {
        [GRHUD showMessage:@"联系人信息错误"];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
